-- summoner
-- create by panyl
-- 技能: 召唤师


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 已召唤次数
        local summonTimes = source.dbase:query("summon_times");
        local maxTimes = para[2];

        if summonTimes  == nil then
            summonTimes = 0;
        end
        -- 达到召唤上限，则不再召唤
        -- 填-1认为是无上限不限制
        if maxTimes ~= -1 and summonTimes >= maxTimes then
            return {};
        end

        -- 如果已经死亡了
        if source:isDead() then
            return {};
        end

        -- 召唤数量
        local elements = para[1];
        local hasSummoned = false;
        local amount = para[3];
        local rand, pos, element;
        for i = 1, amount do
            local arr = DungeonM.findEmptyGrids();
            -- 如果没有空位
            if #arr <= 0 then
                break;
            end

            rand = DungeonM.getRandSeed("summoner_skill");

            pos = arr[rand % #arr + 1];

            -- 随机选一个召唤物
            element = elements[rand % #elements + 1];

            -- 召唤一个怪物
            DungeonM.transGridToMonster(pos, element);

            hasSummoned = true;
        end

        -- 如果没有进行过召唤，不作记录
        if not hasSummoned then
            return {};
        end

        -- 记录召唤次数
        summonTimes = summonTimes + 1;
        source.dbase:set("summon_times", summonTimes);

        -- 如果有召唤上限并且未记录，则记录一下用于倒计时显示
        if maxTimes > 0 and not source.dbase:query("max_summon_times") then
            source.dbase:set("max_summon_times", maxTimes);
        end

        -- 如果需要标记预警
        local dbase = SkillM.query(skillId, "dbase") or {};
        if 1 == dbase["monster_warning"] then
            DungeonM.setDungeonMixedField("monster_warning", 1);
        end

        return { source, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        local elements = string.explode(arr[1], "+");
        for index, element in pairs(elements) do
            elements[index] = tonumber(element);
        end

        return {elements, tonumber(arr[2]), tonumber(arr[3])};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
}

